package com.lc.pianostudent.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.lc.pianostudent.BaseApplication;
import com.lc.pianostudent.R;
import com.lc.pianostudent.conn.PostCustomerIndex;
import com.lc.pianostudent.conn.PostLogin;
import com.lc.pianostudent.fragment.HomeFragment;
import com.lc.pianostudent.util.Validator;
import com.zcx.helper.app.AppCallBack;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilApp;
import com.zcx.helper.util.UtilToast;
import com.zcx.helper.view.AppPassword;
import com.zcx.helper.view.AppUsername;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2018/6/20.
 */

public class LoginActivity extends BaseActivity implements View.OnClickListener{

    private TimerTask timerTask = null;
    private Timer timer = null;
    private boolean isExit = false;
    @BoundView(value = R.id.login_id,isClick = true) private LinearLayout login_button;
    @BoundView(R.id.login_input_phone) private AppUsername login_input_phone; //手机号
    @BoundView(R.id.login_password_phone) private AppPassword login_password_phone; //密码
    @BoundView(value = R.id.register_id,isClick = true) private TextView register_id;
    @BoundView(value = R.id.forget_pass_id,isClick = true) private TextView forget_pass_id;
//    private int remember = 1;
    @BoundView(value = R.id.read_choose,isClick = true) private ImageView read_choose;
    @BoundView(value = R.id.tele,isClick = true) private TextView tele;

    //登录接口
    private PostLogin postLogin = new PostLogin(new AsyCallBack<PostLogin.PostLoginInfo>() {

        @Override
        public void onSuccess(String toast, int type, Object o, PostLogin.PostLoginInfo postLoginInfo) throws Exception {
            if (postLoginInfo.code.equals("200")) {

                BaseApplication.BasePreferences.saveUID(postLoginInfo.user_id);
                BaseApplication.BasePreferences.setIsLogin(true);
                BaseApplication.BasePreferences.saveHeadImg(postLoginInfo.avatar);
                BaseApplication.BasePreferences.setLoginPass(login_password_phone.getText().toString().trim());
                BaseApplication.BasePreferences.savePhone(login_input_phone.getText().toString().trim());
                BaseApplication.BasePreferences.saveName(postLoginInfo.name);
                BaseApplication.BasePreferences.savePerson(postLoginInfo.person);
                BaseApplication.BasePreferences.saveTeacherId(postLoginInfo.teacherid);
                BaseApplication.BasePreferences.saveTeacherName(postLoginInfo.teacher);
                UtilToast.show(toast);
                if (NavigationActivity.onClickProduct != null) {
                    NavigationActivity.onClickProduct.setOnClick();
                }

                try {
                    ((NavigationActivity.CallBack)getAppCallBack(NavigationActivity.class)).onRefresh();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                finish();

            } else {

                UtilToast.show(toast);

            }

        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            UtilToast.show(toast);
        }
    });

    //获取客服电话
    private PostCustomerIndex postCustomerIndex = new PostCustomerIndex(new AsyCallBack<PostCustomerIndex.PostCustomerIndexInfo>() {

        @Override
        public void onSuccess(String toast, int type, Object o, PostCustomerIndex.PostCustomerIndexInfo postCustomerIndexInfo) throws Exception {
            tele.setText(postCustomerIndexInfo.tel);
            BaseApplication.BasePreferences.saveKefu(postCustomerIndexInfo.tel);

        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            UtilToast.show(toast);
        }
    });
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();
    }

    private void initView(){
        postCustomerIndex.execute(false);
        setAppCallBack(new CallBack());
        if(BaseApplication.BasePreferences.readIsRememer()){
            read_choose.setImageResource(R.mipmap.login_btn_checked);
            if(!BaseApplication.BasePreferences.getLoginPass().equals("")){
                login_password_phone.setText(BaseApplication.BasePreferences.getLoginPass());
                login_input_phone.setText(BaseApplication.BasePreferences.readPhone());
            }
        }else {
            read_choose.setImageResource(R.mipmap.login_btn_default);
        }


    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.login_id:
                if (TextUtils.isEmpty(login_input_phone.getText().toString().trim())) {
                    UtilToast.show("请输入手机号");
                } else if (!Validator.isMobile(login_input_phone.getText().toString().trim())) {
                    UtilToast.show("手机号格式错误");
                } else if (TextUtils.isEmpty(login_password_phone.getText().toString().trim())) {
                    UtilToast.show("请输入登录密码");
                } else if (!Validator.isPassword2(login_password_phone.getText().toString().trim())) {
                    UtilToast.show("请输入6-12位英文、数字");
                } else {
                    postLogin.username = login_input_phone.getText().toString().trim();
                    postLogin.password = login_password_phone.getText().toString().trim();
                    postLogin.type = "0";
                    postLogin.execute();

                }
                break;



            case R.id.register_id:

                startVerifyActivity(RegisterActivity.class);

                break;

            case R.id.forget_pass_id:

                startVerifyActivity(ForgetPassActivity.class);

                break;

            case R.id.read_choose:
                if(BaseApplication.BasePreferences.readIsRememer()){
                    read_choose.setImageResource(R.mipmap.login_btn_default);
//                    remember = 0;
                    BaseApplication.BasePreferences.saveIsRememer(false);
                }else {
                    read_choose.setImageResource(R.mipmap.login_btn_checked);
//                    remember = 1;
                    BaseApplication.BasePreferences.saveIsRememer(true);
                }

                break;

            case R.id.tele:
                UtilApp.call(BaseApplication.BasePreferences.readKefu());
                break;

        }
    }


    public interface VerifyCallBack {

        void onCallBack();

    }

    public static void StartVerifyActivity(VerifyCallBack verifyCallBack) {

        if (!BaseApplication.BasePreferences.getIsLogin()) {

            BaseApplication.INSTANCE.currentActivity().startActivity(new Intent(BaseApplication.INSTANCE.currentActivity(), LoginActivity.class));

        } else {

            verifyCallBack.onCallBack();

        }

    }

    @Override
    public void onBackPressed() {
        timer = new Timer();
        if (isExit) {
            BaseApplication.INSTANCE.finishAllActivity();

        } else {
            isExit = true;
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            timerTask = new TimerTask() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    isExit = false;
                }
            };
            timer.schedule(timerTask, 2000);
        }

    }

    public class CallBack implements AppCallBack {

        public void onRefresh() {
            Log.e("====login","---login");
            if(BaseApplication.BasePreferences.readIsRememer()){
                read_choose.setImageResource(R.mipmap.login_btn_checked);
                if(!BaseApplication.BasePreferences.getLoginPass().equals("")){
                    login_password_phone.setText(BaseApplication.BasePreferences.getLoginPass());
                    login_input_phone.setText(BaseApplication.BasePreferences.readPhone());

                }
            }else {
                read_choose.setImageResource(R.mipmap.login_btn_default);
            }
        }

    }
}
